package com.example.rabbitmqboot.consumer;

import com.alibaba.fastjson.JSON;
import com.rabbitmq.client.Channel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
@Slf4j
public class RabbitConsumer {

    @RabbitListener(
            containerFactory = "rabbitListenerContainerFactory",
            bindings = @QueueBinding(
                    value = @Queue(value = "SchoolQueue", durable = "true"),
                    exchange = @Exchange(value = "SchoolExchange", durable = "true", type = "topic"),
                    key = "homework.#"
            )
    )
    public void mathMsg(Message message, Channel channel) throws IOException, InterruptedException {
        //这里 message body里面是字节数组，解码一下就行了
        log.info("1线程：{}，收到作业信息：{}", Thread.currentThread().getId(), new String(message.getBody()));

        Thread.sleep(500);

        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
    }

    @RabbitListener(
            containerFactory = "rabbitListenerContainerFactory",
            bindings = @QueueBinding(
                    value = @Queue(value = "SchoolQueue", durable = "true"),
                    exchange = @Exchange(value = "SchoolExchange", durable = "true", type = "topic"),
                    key = "homework.#"
            )
    )
    public void mathMsg2(Message message, Channel channel) throws IOException, InterruptedException {
        //这里 message body里面是字节数组，解码一下就行了
        log.info("2线程：{}，收到作业信息：{}", Thread.currentThread().getId(), new String(message.getBody()));

        Thread.sleep(500);

        channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, false);
    }
}
